package com.atlin.project.model.domain.chart;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description chart 原始数据表
 * @createDate 2024/11/20 17:19
 */
@Data
public class ChartOriginalData {
    /**
     * 数据表头
     */
    private List<String> headers;

    /**
     * 数据
     */
    private List<Map<String, Object>> rows;


    /**
     * ChartCsvData ---> csv
     * 去除 index 列
     */
    public static String objToCsv(ChartOriginalData chartOriginalData) {
        if (chartOriginalData == null || chartOriginalData.getHeaders().size() <= 1) return "";

        final String INDEX = "idx";
        List<String> headers = chartOriginalData.getHeaders();
        headers.remove(INDEX);
        List<Map<String, Object>> rows = chartOriginalData.getRows();
        String csvHeaderData = String.join(",", headers) + "\n";

        StringBuilder builder = new StringBuilder(csvHeaderData);
        for (Map<String, Object> rowMap : rows) {
            String csvRow = headers.stream()
                    .map(key -> String.valueOf(rowMap.get(key)))
                    .collect(Collectors.joining(",")) + "\n";
            builder.append(csvRow);
        }

        return builder.toString();
    }

    /**
     * csv --> ChartCsvData
     * 添加 index 列
     */
    public static ChartOriginalData csvToObj(String csv) {
        if (StringUtils.isBlank(csv)) return null;
        String[] csvArr = csv.split("\\n");
        if (csvArr.length < 2) {
            return null;
        }
        ChartOriginalData chartOriginalData = new ChartOriginalData();
        final String INDEX = "idx";
        // 表头
        List<String> headers = new ArrayList<>();
        headers.add(0, INDEX);
        headers.addAll(Arrays.asList(csvArr[0].split(",")));
        // 数据行
        List<Map<String, Object>> rows = new ArrayList<>();
        for (int i = 1; i < csvArr.length; i++) {
            HashMap<String, Object> row = new HashMap<>();
            row.put(INDEX, i);
            String[] rowArr = csvArr[i].split(",");
            int maxRowArrIndex = rowArr.length - 1;
            for (int j = 1; j < headers.size(); j++) {
                Object value = j - 1 > maxRowArrIndex ? null : rowArr[j - 1];
                row.put(headers.get(j), value);
            }
            rows.add(row);
        }
        chartOriginalData.setHeaders(headers);
        chartOriginalData.setRows(rows);
        return chartOriginalData;
    }

    /**
     * 将数据库查询出来的数据构建为 ChartCsvData
     */
    public static ChartOriginalData build(List<Map<String, Object>> chartData) {
        if (chartData != null && chartData.size() > 1) {
            // 首行表头 
            Map<String, Object> map = chartData.get(0);

            ChartOriginalData chartOriginalData = new ChartOriginalData();
            chartOriginalData.setRows(chartData);
            chartOriginalData.setHeaders(new ArrayList<>(map.keySet()));
            return chartOriginalData;
        }
        return null;
    }
}
